/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 2 -*- */
/**
* @headerfile usbserial.hpp
* 
* Header for easy comunication through USB ( Serial ).
* 
* @author Miguel Ángel Vílchez López 2009 <sacra@openmatica.es>
*
* Codigo is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* 
* Codigo is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License along
* with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef _USBSERIAL_HPP_
#define _USBSERIAL_HPP_

#include <stdint.h>
#include <errno.h> // A quitar con el debug
#include <termios.h>
#include <sys/ioctl.h>
#include <getopt.h>
#include "debug.hpp"

/**
* @class CMUsbSerial
* @brief Comunication through USB ( Serial ).
*/

class CMUsbSerial
{

	private:

		/**
		* The log class.
		*/
		CMDebug debug;
		
		/**
		* The /dev/ttyUSB
		*/
                char* port;
		
		/**
		* The bauds.
		*/
                int baud;

                /**
                * The file descriptor
                */
                int fd;
                
		/**
		* Converts an array of chars (10 caracters max.) into numbers.
		*
		* @return Returns the number of the word.
		*/
		int init();
	
	public:
	
		/**
		* Constructor
		*
		* @param[in] id The identificator who identifies the portion of memory shared.
		* @param[in] buffer The buffer size.
                * @param[in] defval The default value.
		* @param[in] debug If the debug is activated.
		* @param[in] filename The file for logging.
		*/
		CMUsbSerial(const char* port,int baud,bool debug,const char* filename);
		
		/**
		* Destructor
		*/
		~CMUsbSerial();
		
		/**
		* Read from the USB ( Serial ).
		*
                * @param[out] data Variable to put data.
                * @param[in] end The end character.
		* @return Returns 0 if succesfull, -1 if not.
		*/
		int read_until(char* data,char end);

                /**
		* Read from the USB ( Serial ).
		*
                * @param[out] data Variable to put data.
                * @param[in] nbytes The number of bytes.
		* @return Returns 0 if succesfull, -1 if not.
		*/
		int read_bytes(char* data,int nbytes);

                /**
		* Read from the USB ( Serial ).
		*
                * @param[out] data Variable to put data.
                * @param[in] begin The begin character.
                * @param[in] end The end character.
		* @return Returns 0 if succesfull, -1 if not.
		*/
		int read_fromto(char* data,char begin,char end);

		/**
		* Write data to USB ( Serial ).
		*
		* @param[in] data Variable data from.
		* @return Returns 0 if succesfull, -1 if not.
		*/
		int write_bytes(char *data);

};

#endif // _USBSERIAL_HPP_
